package othello.server.controller;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import othello.server.service.OthelloServiceImpl;
import othello.shared.model.StateAtLounge;

public class OthelloController extends Controller {

	private static final OthelloServiceImpl SERVICE = new OthelloServiceImpl();

	/**
	 * セッションIDにゲーム盤が紐づいていればゲーム画面に移行。それ以外ではトップ画面へ。
	 */
	@Override
	public Navigation run() throws Exception {

		StateAtLounge state = SERVICE
				.getGameBoard(request.getSession().getId());
		if (state != null) {
			requestScope("gameBoardId", state.getGameBoardId());
			requestScope("mySide", state.getSide());
		}
		return (state != null && state.getGameBoardId() != null) ? forward("othello.jsp")
				: redirect("/");
	}
}
